//
//  DZMainTabBar.swift
//  dazhanweibo
//
//  Created by apple on 16/2/28.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit

@objc protocol DZMainTabBarDelegate:NSObjectProtocol{
    
    //可选
    optional
    func mainTabBarComposeClick()
}

class DZMainTabBar: UITabBar {

    weak var composeDelegate: DZMainTabBarDelegate?
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let width = frame.width / 5
        composeButton.frame = CGRect(x: 0, y: 0, width: width, height: frame.height)
        composeButton.center = CGPoint(x: frame.width * 0.5, y: frame.height * 0.5)
    }
    
    func buttonClick(button : UIButton) {
        // 3.调用代理
        // composeDelegate?: 表示composeDelegate属性有值才执行后面的代码
        // mainTabBarComposeClick?(): 表示代理实现这个方法就调用代理的这个方法,反之代理没有实现这个方法,就不调用
        self.composeDelegate?.mainTabBarComposeClick?()
    }
    //MARK: - 懒加载
    lazy var composeButton : UIButton = {
        let button = UIButton()
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        self.addSubview(button)
        button.sizeToFit()
        
        button.addTarget(self, action: "buttonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()

}
